package cn.dancepeak.controller;

import cn.dancepeak.domain.Teacher;
import cn.dancepeak.service.ITeacherService;
import cn.dancepeak.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老师信息统一管理控制层
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;

    /**
     * 查询老师的所有信息
     * @return R<list<Teacher>>  老师信息的集合
     */
    @PostMapping("/list")
    public R<List<Teacher>> getTeacher(){
        List<Teacher> list =  teacherService.getTeacher();
        return R.ok(list);
    }

    /**
     * 新增一条老师信息
     * @param teacher
     * @return
     */
    @PostMapping("/add")
    public R addTeacher(@RequestBody Teacher teacher){
        try {
            teacherService.addTeacher(teacher);
            return R.ok("新增成功！");
        }catch (Exception e){
            e.printStackTrace();
            return R.fail("mmm");
        }
    }

    /**
     * 修改一条老师信息
     * @param teacher
     * @return
     */
    @PostMapping("/update")
    public R updateTeacher(@RequestBody Teacher teacher){
        teacherService.updateTeacher(teacher);
        return R.ok("修改成功！");
    }

    /**
     * 通过id删除一条老师信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteTeacherById(@PathVariable Long id){
        try {
            teacherService.deleteTeacherById(id);
            return R.ok("删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }

    }

}
